package com.cuiweiyou.bluemesh.model;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;

import com.cuiweiyou.bluemesh.R;

public class NotificationModel {
    private int notificationdId;
    private Notification notification;

    public NotificationModel(NotificationManager notificationManager, Context context, int notificationdId, String contentTitle, String contentText, int icon) {
        this.notificationdId = notificationdId;

        String channelId = context.getPackageName();              // 通知渠道的ID

        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());

        PendingIntent pendingIntent = PendingIntent.getActivity(context,         // 上下文
                                                                1,   // 请求码
                                                                intent,          // 目的
                                                                PendingIntent.FLAG_UPDATE_CURRENT);   // 模式

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 26
            String channelname = context.getString(R.string.app_name);           // 通知的渠道名
            String description = context.getString(R.string.notification_desc);  // 通知的渠道描述
            NotificationChannel notificationChannel = new NotificationChannel(channelId, channelname, NotificationManager.IMPORTANCE_HIGH); // 构建NotificationChannel实例
            notificationChannel.setDescription(description);  // 配置通知渠道的属性

            notificationManager.createNotificationChannel(notificationChannel); // 在notificationManager中创建通知渠道

            notification = new NotificationCompat.Builder(context, channelId) //
                    .setContentTitle(contentTitle)         // 通知标题
                    .setContentText(contentText)           // 通知内容
                    .setWhen(System.currentTimeMillis())   // 通知被创建的时间
                    .setSmallIcon(R.mipmap.ico_house)      // 通知的小图标
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ico_house))    // 通知的大图标
                    .setContentIntent(pendingIntent)  // 点击跳转通知跳转
                    .setAutoCancel(false)             // 实现点击跳转后关闭通知
                    .build();
        } else {
            notification = new NotificationCompat.Builder(context, channelId) //
                    .setContentTitle(contentTitle)  //
                    .setContentText(contentText)    //
                    .setWhen(System.currentTimeMillis())   //
                    .setSmallIcon(R.mipmap.ico_house)      //
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ico_house))   //
                    .setContentIntent(pendingIntent)   //
                    .setAutoCancel(false)   //
                    .build();
        }
    }

    public Notification getNotification() {
        return notification;
    }

    public int getNotificationdId() {
        return notificationdId;
    }
}
